package com.hetao.xx.orm;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

public class PlaceNpcDao {
    private final String Tag= getClass().getSimpleName();

    private final Context context;

    public PlaceNpcDao(Context context)  {
        this.context = context;
    }

    public DatabaseHelper getHelper() {
        return DatabaseHelper.getInstance(context);
    }

    public Dao<PlaceNpcVo, Integer> getDao() throws SQLException {
        //传入实体类拿到这个实体类的Dao对象
        return getHelper().getDao(PlaceNpcVo.class);
    }

    public int add(PlaceNpcVo bean) {
        try {
            return getDao().create(bean);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int update(PlaceNpcVo bean) {
        try {
            return getDao().update(bean);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }


    public PlaceNpcVo queryByName(String name) {
        try {
            return getDao().queryBuilder().where().eq("name", name).queryForFirst();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public List<PlaceNpcVo> queryByPlaceId(int placeId) {
        try {
            return getDao().queryBuilder().where().eq("place_id", placeId).query();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public List<PlaceNpcVo> queryAll() {
        try {
            return getDao().queryForAll();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public long countAll() {
        try {
            return getDao().countOf();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int delById(int id) {
        try {
            return getDao().deleteById(id);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int dropTable() {
        try {
            getDao().queryRaw("delete from place_npc");
            getDao().queryRaw("update sqlite_sequence SET seq = 0 where name ='place_npc'");
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }
}
